﻿/**
 * DataDrivenFactory.cs
 *
 * Copyright (C) 2008,  iron9light
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections;
//using System.Collections.Generic;
using System.Reflection;
using NUnit.Core;

namespace NUnitExtension.DataDriven.AddIn
{
    public class DataDrivenFactory
    {
        public DataDrivenSuite CreateSuite(MethodInfo method)
        {
            if (method == null)
                throw new ArgumentNullException("method");

            DataDrivenSuite testSuite = new DataDrivenSuite(method);
            NUnit.Core.NUnitFramework.ApplyCommonAttributes(method, testSuite);

            return testSuite;
        }

        //public IList<DataDrivenTestCase> GreateTestCases(MethodInfo method, Attribute dataFactory)
        public IList GreateTestCases(MethodInfo method, Attribute dataFactory)
        {
            if(method == null)
                throw new ArgumentNullException("method");

            if(dataFactory == null)
                throw new ArgumentNullException("dataFactory");

            object factory = DataDrivenFramework.GetFactory(dataFactory);
            object[] args = DataDrivenFramework.GetArguments(dataFactory);
            IEnumerable testCaseItems = DataDrivenFramework.GetTestCaseItems(factory, args);
            
            //List<DataDrivenTestCase> list = new List<DataDrivenTestCase>();
            ArrayList list = new ArrayList();
            if(testCaseItems!= null)
            {
                foreach (object testCaseItem in testCaseItems)
                {
                    if(DataDrivenFramework.IsTestCaseItem(testCaseItem))
                    {
                        object[] arguments = DataDrivenFramework.GetArguments(testCaseItem);
                        IEnumerable constraints = DataDrivenFramework.GetConstraints(testCaseItem);
                        if(constraints == null)
                        {
                            DataDrivenTestCase testCase = new DataDrivenTestCase(method, arguments, null);
                            list.Add(testCase);
                        }
                        else
                        {
                            DataDrivenTestCase testCase = new DataDrivenTestCase(method, arguments, ToArray(constraints));
                            list.Add(testCase);
                        }
                    }
                }
            }
            return list;
        }

        static object[] ToArray(IEnumerable constraints)
        {
            ArrayList list = new ArrayList();
            foreach (object constraint in constraints)
            {
                list.Add(constraint);
            }
            return list.ToArray();
        }
    }
}
